//
//  leftSubMainVC.m
//  dataGathering
//
//  Created by apple on 14-11-4.
//  Copyright (c) 2014年 shuili. All rights reserved.
//

#import "leftSubMainVC.h"
#import "mainVC.h"
#import "myTaskCollectionTableVC.h"
#import "WorksetUploadManager.h"

@interface leftSubMainVC ()

@end

@implementation leftSubMainVC

DXPopover *popover;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)addButtonClicked{
    
    [((mainVC*)(self.parentViewController)) addNewTaskCollection];
}

-(void)searchButtonClicked{
    [_searchButton setAlpha:0];
    
    [UIView animateWithDuration:0.2 animations:^{
        CGRect formSheetRect = _searchTF.frame;
        formSheetRect.origin.x -= 160;
        _searchTF.frame = formSheetRect;
        
        
    } completion:^(BOOL finished) {
        [_searchTF becomeFirstResponder];
    }];
}



-(void)textFieldDidEndEditing:(UITextField *)textField
{
    //[((myTaskCollectionTableVC*)[[self childViewControllers] objectAtIndex:0]) refreshTaskCollectionTableView];
    [UIView animateWithDuration:0.2 animations:^{
        CGRect formSheetRect = _searchTF.frame;
        formSheetRect.origin.x += 160;
        _searchTF.frame = formSheetRect;
        
        
    } completion:^(BOOL finished) {
        [_searchButton setAlpha:1];
    }];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _addButton = [[UIButton alloc] initWithFrame:CGRectMake(20, 108+54, 56, 56)];
    [_addButton setImage:[UIImage imageNamed:@"u1131.png"] forState:UIControlStateNormal];
    [_addButton setImage:[UIImage imageNamed:@"u1131_mouseOver.png"] forState:UIControlStateHighlighted];
    [_addButton addTarget:self action:@selector(addButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_addButton];
    
    _searchButton = [[UIButton alloc] initWithFrame:CGRectMake(640-24-24, 136, 24, 24)];
    [_searchButton setImage:[UIImage imageNamed:@"u664.png"] forState:UIControlStateNormal];
    [_searchButton setImage:[UIImage imageNamed:@"btn_查询_u850_selected.png"] forState:UIControlStateHighlighted];
    [_searchButton addTarget:self action:@selector(searchButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_searchButton];

    [_searchTF setDelegate:self];
    
    [_searchTF addTarget:self action:@selector(textChangeAction:) forControlEvents:UIControlEventEditingChanged];
    // Do any additional setup after loading the view.
    
}

- (void) textChangeAction:(id) sender {
    if(![[_searchTF text] isEqualToString:@""]){
        NSArray *array = [[currentWorkSetManager sharedInstance] getWorksetArray];
        NSMutableArray *searchArray = [[NSMutableArray alloc] initWithCapacity:10];
        for(workSet *tempWorkset in array){
            if([[tempWorkset name] containsString:[_searchTF text]]){
                [searchArray addObject:tempWorkset];
            }
        }
        [((myTaskCollectionTableVC*)[[self childViewControllers] objectAtIndex:0]) setWorkSetArray:searchArray];
        [[((myTaskCollectionTableVC*)[[self childViewControllers] objectAtIndex:0]) tableView] reloadData];
    } else {
        [((myTaskCollectionTableVC*)[[self childViewControllers] objectAtIndex:0]) refreshTaskCollectionTableView];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)exitAction{
    [popover dismiss];
    [self.parentViewController dismissViewControllerAnimated:YES completion:^{
    }];
    
}

- (IBAction)menuButtonClicked:(id)sender {
    NSLog(@"mm");
    
    UIViewController *mainMenuVC = [self.storyboard instantiateViewControllerWithIdentifier:@"mainMenu"];
    UIButton *exitButton = (UIButton*)[mainMenuVC.view viewWithTag:5];
    UITextField *userNameLabel = (UITextField*)[mainMenuVC.view viewWithTag:1];
    UITextField *userDescLabel = (UITextField*)[mainMenuVC.view viewWithTag:2];
    [exitButton addTarget:self action:@selector(exitAction) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *syncAllWorksetButton = (UIButton*)[mainMenuVC.view viewWithTag:4];
    [syncAllWorksetButton addTarget:self action:@selector(syncAllWorkset) forControlEvents:UIControlEventTouchUpInside];
    mainMenuVC.view.frame = CGRectMake(0.0, 0.0, 240, 230);
    
    NSString *userName = [[NSUserDefaults standardUserDefaults] stringForKey:@"username"];
    NSString *userDesc = [[NSUserDefaults standardUserDefaults] stringForKey:@"department"];
    [userNameLabel setText:userName];
    [userDescLabel setText:userDesc];

    popover = [DXPopover popover];
    //[popover showAtView:sender withContentView:mainMenuVC.view inView:self.parentViewController.view];
    [popover showAtPoint:CGPointMake(66, 70) popoverPostion:DXPopoverPositionDown withContentView:mainMenuVC.view inView:self.parentViewController.view];
    
}

-(void)syncAllWorkset
{
    [popover dismiss];
    [[WorksetUploadManager sharedInstance] newSyncAllWorksets];
}

@end
